'''
$LastChangedBy: c.gillespie@ncl.ac.uk $
$Revision: 123 $
$LastChangedDate: 2009-04-24 17:27:10 +0000 (Fri, 24 Apr 2009) $


If want change the default shape/colour/etc of a node or arrow
make your changes in Default styles - not here
'''
from copy import copy


class DotNodes(object):
    '''A class that adds attributes to nodes and edges'''
    
    def    __init__(self, model, styles):
        self.m = model
        self.styles = styles
    
    
    def edgeNode(self, reaction, stoic, edge_type):
        edge_attribute = copy(self.styles[edge_type])
        
        if reaction.getReversible() == 1 and edge_attribute['reversible']:
            edge_attribute['arrowtail'] = edge_attribute['reversible']
        del edge_attribute['reversible']

        if reaction.getFast() == 1 and edge_attribute['fast']: 
            edge_attribute['color'] = edge_attribute['fast'] 
        del edge_attribute['fast']
        
        if stoic != 1:
            edge_attribute['label'] = str(stoic)

        return edge_attribute
    
    def specieNode(self, i, other_attributes=None):
        if not other_attributes:
            other_attributes = {}
        
        label = get_label(self.m.getSpecies(i-1))

        node_attribute = copy(self.styles['species_node'])
        node_attribute['label'] = '\"' + label + '\"'
        node_attribute['URL'] = "\"Javascript:species('//sbml:species[%s]')\"" % str(i)
        
        #if boundary condition
        if i and self.m.getSpecies(i-1).getBoundaryCondition() and node_attribute['boundaryCondition']:
            node_attribute['color'] = node_attribute['boundaryCondition']
        del node_attribute['boundaryCondition']
        
        for key, value in other_attributes.items():
            node_attribute[key] = value
        return node_attribute
    
    def reactionNode(self, i, other_attributes=None):
        '''See class doc'''
        if not other_attributes:
            other_attributes = {}
        
        label = get_label(self.m.getReaction(i-1))
        
        node_attribute = copy(self.styles['reaction_node'])
        
        node_attribute['label'] = '\"' + label + '\"'
        node_attribute['URL'] = "\"Javascript:reaction('//sbml:reaction[%s]')\"" % str(i)
        for key, value in other_attributes.items():
            node_attribute[key] = value
        return node_attribute
        

    def compartmentNode(self, i, other_attributes=None):    
        '''See class doc'''
        if not other_attributes:
            other_attributes = {}
        
        label = get_label(self.m.getCompartment(i-1))
        
        node_attribute = copy(self.styles['compartment_node'])
        node_attribute['graph_name'] = self.m.getCompartment(i-1).getId()
        node_attribute['label'] = '\"' + label + '\"'
        node_attribute['URL'] = "\"Javascript:compartment(\'//sbml:compartment[%s]\')\"" % str(i)
        
        for key, value in other_attributes.items():
            node_attribute[key] = value
        return node_attribute
        
def get_label(obj):
   
    if len(obj.getName()):
        return obj.getName()
   
    return obj.getId()
